using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;

namespace TVDesigner.Entities.Elements
{
    public class PropertyComboScreenChoserEditor : UITypeEditor
    {
        private IWindowsFormsEditorService service1;

        private class PropertiesList : ListBox
        {
            private IWindowsFormsEditorService _Service;

            public PropertiesList(HButton button, IWindowsFormsEditorService service1)
            {
                _Service = service1;
                foreach (Screen sc in button.ParentScreen.GetProject().Screens)
                {
                    if (!button.ParentScreen.Equals(sc))
                    {
                        this.Items.Add(sc.Name);
                    }
                }

                // Not setting the border to none just doesn't look good.
                this.BorderStyle = BorderStyle.None;
                this.SelectedIndexChanged += new EventHandler(this.SelectedIndexChange);
            }

            private void SelectedIndexChange(object sender, EventArgs e)
            {
                if (_Service != null)
                {
                    _Service.CloseDropDown();
                }
            }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                // This service is in charge of popping our ListBox.
                service1 = ((IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService)));

                if (service1 != null)
                {
                    // This is an internal Microsoft class representing the PropertyGrid entry for our component.
                    if (provider.GetType().FullName == "System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry")
                    {
                        // Get the component we're working on via reflection.
                        //Component comp = ((Component)(provider.GetType().GetProperty("Component").GetGetMethod().Invoke(provider, new object[0])));

                        PropertiesList list = new PropertiesList(context.Instance as HButton, service1);

                        // Drop the list control.
                        service1.DropDownControl(list);

                        if (list.SelectedIndices.Count == 1)
                        {
                            value = list.SelectedItem;
                        }

                        // Close the list control after selection.
                        service1.CloseDropDown();
                    }
                }
             }

            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            // We're using a drop down style UITypeEditor.
            return UITypeEditorEditStyle.DropDown;
        }

    }
}
